/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * This example shows how to set-up a service that other clients can subscribe
 * to, and then data can be sent to those subscribers.
 */

#include "gimi.h"

int main(void) {
  int count = 0;
  std::string hubname = "asrobo.hut.fi";
  int hubport = 50002;
  std::string ownname = "gimitutorialservice";

  // Initialize dPrint.
  debugInit();
  debugSetGlobalDebugLvl(6);
  
  // Create the GIMI-object.
  dPrint(1, "Create object.");
  gimi::GIMI gi;

  // Add a service this client provides.
  // Major number for service determines it's type. Available and used numbers
  // are defined in GIMI/gimiprotocoltypes.h
  // Datatypes are defined in GIMI/datatypes/
  dPrint(1, "Add services");
  printf("Add services\n");
  if (!gi.addProvidedService(8000, 0, "Example service", "Here")) {
    dPrint(1, "Given service number was already added.");
    return -1;
  }
  if (!gi.addProvidedService(8000, 1, "Example service, second instance", "There")) {
    dPrint(1, "Given service number was already added.");
    return -1;
  }
       
  // Connect to tcphub.
  if (!gi.connectToHub(hubname, hubport, ownname)) {
    dPrint(1, "Could not connect to tcpHub. Are connection parameters correct?");
    return -1;
  }

  // Create some data.
  char *data = new char[32];
  strcpy(data, "abcdefghijklmn");
  
  while (count < 100) {
    // Send data to subscribers if there are any.
    if (gi.getSubscriberCount(8000, 0) > 0) {
      // Actually send the given data.
      int result = gi.sendToSubscribers(data, 15, 8000, 0);
      if (result != GIMI_OK) {
        dPrint(1, "Sending data to subscribers failed with error %d (%s)!",
               result, gimi::getGimiErrorString(result).c_str());
        delete data;
        return -1;
      } else {
        ++count;
      }
    } else {
      dPrint(1, "No subscribers for 8000:0. No data sent.");
    }
    if (gi.getSubscriberCount(8000, 1) > 0) {
      // Actually send the given data.
      int result = gi.sendToSubscribers(data, 15, 8000, 1);
      if (result != GIMI_OK) {
        dPrint(1, "Sending data to subscribers failed with error %d (%s)!",
               result, gimi::getGimiErrorString(result).c_str());
        delete data;
        return -1;
      } else {
        ++count;
      }
    } else {
      dPrint(1, "No subscribers for 8000:1. No data sent.");
    }
    ownSleep_ms(250);
  }

  delete data;
  
  // GIMI-destructor handles disconnecting

  // Closes logfile in case if it was used. (dPrint)
  debugDeinit();
  
  return 0;
}
